package org.jash.mvp2206.news;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.MaterialToolbar;

import org.jash.mvp2206.R;
import org.jash.mvp2206.entity.DetailEntity;
import org.jash.mvp2206.mvp.BaseActivity;

public class DetailActivity extends BaseActivity<Construct.IDetailView, DetailPresenter> implements Construct.IDetailView {
    private MaterialToolbar toolbar;
    private TextView content;
    private ImageView image;
    private int id;
    private CommentFragment dialog;

    @Override
    protected int layoutId() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initData() {
        presenter = new DetailPresenter();
        id = getIntent().getIntExtra("id", 0);
        presenter.getNewsDetail(id);
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
        content = findViewById(R.id.content);
        image = findViewById(R.id.news_image);
        dialog = new CommentFragment();
        dialog.setNid(id);
    }

    @Override
    public void loadDetailSuccess(DetailEntity.DataBean bean) {
        toolbar.setTitle(bean.getTitle());
        Glide.with(image).load(bean.getImgurl()).into(image);
        content.setText(bean.getContent());
        findViewById(R.id.comment).setOnClickListener(v -> dialog.show(getSupportFragmentManager(), null));
    }

    @Override
    public void failure(Throwable t) {
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}